# Solution Rename PowerShell Script
# Usage: PS C:\path\with\script\and\template\to\rename>  .\TemplateRename.ps1 stringToReplace replaceWith
# Renames all folders, files and strings within files

$a = $args.length

if ($a -ne 2) {write-warning "You must specify two parameters: stringToReplace replaceWith"; break}

$replaceMe = $args[0]
$replaceWith = $args[1]

# Build an array list of directories to rename
$dirList = New-Object System.Collections.ArrayList
gci -recurse -include *$replaceMe* | Where {$_.psIsContainer -eq $true} | foreach { $dirList.Add($_.FullName); }

# reverse-sort the list
$dirList = $dirList | sort-object -descending

# rename the directories that are stored in the list
$dirList | foreach { rename-item -path $_ -newname $(split-path $_ -leaf).Replace($replaceMe, $replaceWith) }

#rename files
gci -recurse -include *$replaceMe*.* | Where {$_.psIsContainer -eq $false} | foreach { rename-item $_ $_.FullName.Replace($replaceMe, $replaceWith) }

#rename IN files
gci -recurse -include *.* -exclude *.ps1, *.dll | Where {$_.psIsContainer -eq $false} | ForEach-Object { [System.IO.File]::WriteAllLines($_.FullName, [System.IO.File]::ReadAllText($_.FullName).Replace($replaceMe, $replaceWith)) }
